package com.eascs.web.o2o.scheduler.service;

import javax.inject.Inject;

import org.springframework.stereotype.Service;

import com.eascs.app.log.Logger;
import com.eascs.app.log.LoggerFactory;
import com.eascs.common.util.StopWatch;
import com.eascs.web.o2o.scheduler.entity.HrUnit;
import com.eascs.web.o2o.scheduler.entity.O2oEmployee;
import com.eascs.web.o2o.scheduler.entity.O2oOrgan;
import com.eascs.web.o2o.scheduler.entity.PubDatadictionary;
import com.eascs.web.o2o.scheduler.entity.PubDditem;

@Service
public class SynchroWorkflow {

	protected final Logger logger = LoggerFactory.getLogger(getClass());

	@Inject
	WorkflowService workflowService;
	public void saveData() {
		StopWatch stopWatch = new StopWatch();
		logger.info("*******************[WORKFLOW]同步数据开始*******************");
		//savePubDatadictionary();
		//savePubDditem();
		//saveHrUnit();
		saveEmployee();
		//saveOrgan();
		
		logger.info("*******************[WORKFLOW]同步数据结束，共耗时：[{}]分*******************", stopWatch.elapsedTime()/60000);
	}
	
	public void savePubDatadictionary() {
		StopWatch stopWatch = new StopWatch();
		logger.info("[WORKFLOW]同步PubDatadictionary开始......");
		try {
			workflowService.loadData("SELECT * from eadb.pub_datadictionary order by ddno",PubDatadictionary.class);
			logger.info("[WORKFLOW]同步PubDatadictionary结束，共耗时：[{}]分", stopWatch.elapsedTime()/60000);
		} catch (Exception e) {
			e.printStackTrace();
			logger.error("[WORKFLOW]Error：同步PubDatadictionary失败！！！");
		}
	}
	
	public void savePubDditem() {
		StopWatch stopWatch = new StopWatch();
		logger.info("[WORKFLOW]同步PubDditem开始......");
		try {
			workflowService.loadData("SELECT * from eadb.pub_dditem order by itemno",PubDditem.class);
			logger.info("[WORKFLOW]同步PubDditem结束，共耗时：[{}]分", stopWatch.elapsedTime()/60000);
		} catch (Exception e) {
			e.printStackTrace();
			logger.error("[WORKFLOW]Error：同步PubDditem失败！！！");
		}
	}
	
	public void saveHrUnit() {
		StopWatch stopWatch = new StopWatch();
		logger.info("[WORKFLOW]同步HrUnit开始......");
		try {
			workflowService.loadData("SELECT * from eadb.hr_units order by unitno",HrUnit.class);
			logger.info("[WORKFLOW]同步HrUnit结束，共耗时：[{}]分", stopWatch.elapsedTime()/60000);
		} catch (Exception e) {
			e.printStackTrace();
			logger.error("[WORKFLOW]Error：同步HrUnit失败！！！");
		}
	}

	/**
	 * 同步员工表数据
	 */
	public void saveEmployee() {
		StopWatch stopWatch = new StopWatch();
		logger.info("[WORKFLOW]同步员工表数据开始......");
		String sql = "SELECT e.employeeno AS employee_no, e.telephone, e.email, e.employeecode AS employee_code,"
				+ " e.employeenamecn AS employee_namecn, e.employeeaccount AS employee_account, e.nodeid AS node_id, e.mobile AS mobile,"
				+ " e.operationdept AS operation_dept, e.creatorno AS creator_no,"
				+ " e.modifierno AS modifier_no, e.createdate AS create_date, "
				+ "e.modicontactdate AS modify_date,e.employeestatus as employee_status FROM eadb.hr_employee e  ORDER BY e.employeeno";
		try {
			workflowService.loadData(sql, O2oEmployee.class);
			logger.info("[WORKFLOW]同步员工表数据结束，共耗时：[{}]分", stopWatch.elapsedTime()/60000);
		} catch (Exception e) {
			e.printStackTrace();
			logger.error("[WORKFLOW]Error：同步员工表数据失败！！！");
		}
	}
	
	/**
	 * 同步组织架构表数据
	 */
	
	public void saveOrgan() {
		StopWatch stopWatch = new StopWatch();
		logger.info("[WORKFLOW]同步组织架构表开始......");
		String sql = "SELECT o.nodeno AS node_no, o.deptcode AS dept_code, o.nodenamecn AS namecn, o.nodeidpath AS node_id_path, o.parentno AS parent_no, o.nodecode AS node_code, o.isdel AS is_del, o.creator AS creator_no, o.creator AS modifier_no, o.createdate AS create_date, sysdate AS modify_date FROM eadb.hr_organ o ORDER BY o.nodeno";
		try {
			workflowService.loadData(sql, O2oOrgan.class);
			logger.info("[WORKFLOW]同步组织架构表结束，共耗时：[{}]分", stopWatch.elapsedTime()/60000);
		} catch (Exception e) {
			e.printStackTrace();
			logger.error("[WORKFLOW]Error：同步组织架构表失败！！！");
		}
	}
	
}
